package cn.pconline.autoclub.service.bbssync;

import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.entity.UserLimit;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;
import cn.pconline.autoclub.service.UserLimitService;
import cn.pconline.autoclub.util.dyna.PostReply;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月4日 上午10:52:48
 */
public class SyncReplyTopicService extends BbsSyncPetrolService {
	@Autowired
	private DynaService dynaService;
	
	@Autowired
	private PetrolService petrolService;
	
	@Autowired
    private UserLimitService limitService;
	
	@Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
	    ClubIntegral clubIntegral = getClubIntegral(msg);
	    String reason = clubIntegral.getName() + "(topic-" + getTopicId(msg) + ")";
        petrolService.addOnlyPersonWithinLimit(clubMember, (int)clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, true);
        return clubIntegral.getPetrol();
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        long userId = getUserId(msg);
        if(userId > 0) {
            ClubMember clubMember = memberService.findClubMemberByUserId(userId);
            if(clubMember != null) {
                UserLimit userLimit = limitService.getMemberLimit(clubMember.getId());
                if(userLimit.getPetrol() >= UserLimit.DAILY_PERSON_LIMIT) {
                    log.info("user[{}] reach the limit[{}]petrol, no longer add petrol to club!", clubMember.getUid(), UserLimit.DAILY_PERSON_LIMIT);
                    return 0;
                }
            }
        }
        ClubIntegral clubIntegral = getClubIntegral(msg);
        String reason = clubIntegral.getName() + "(topic-" + getTopicId(msg) + ")";
        petrolService.addToClubWithinLimit(club.getClubId(), (int)clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, true);
        return clubIntegral.getPetrol();
    }

    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("postAuthorId");
    }
	
	@Override
	public void process(JSONObject msg, ClubMember clubMember) {
	    
		//验证用户
	    long userId = getUserId(msg);
	    if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }

        long clubId = 0;
        if (clubMember != null) {
            clubId = clubMember.getClubId();
        }
		
        //回帖同步
        PostReply postReply = parseActionLog2PostReply(msg);
        dynaService.replyTopicDyna(postReply);
        dynaService.updateLastReplyInfoByTopicId(
                postReply.getTopicId(), clubId, postReply.getTopicCreateAt(),
                postReply.getUserId(), postReply.getCreateName(), postReply.getCreateAt());
		dynaService.setJson4BipBbsTopics(postReply);
	}
	
	/**
     * actionLog转回复实体,回复帖子同步有效
     *
     * @param bbsActionLog
     * @return
     */
    public PostReply parseActionLog2PostReply(JSONObject msg) {
        PostReply postReply = new PostReply();
        postReply.setCreateAt(msg.getDate("lastPostAt"));
        postReply.setCreateName(msg.getString("postAuthorName"));//论坛传的是nickname
        postReply.setFloor(msg.getIntValue("postFloor"));
        postReply.setMessage(msg.getString("postMessage"));
        postReply.setPostId(msg.getLongValue("postId"));
        postReply.setTopicCreateAt(msg.getDate("topicCreateAt"));
        postReply.setTopicUserId(msg.getLongValue("topicAuthorId"));
        postReply.setTopicTitle(msg.getString("title"));
        //引用
        boolean isQuote = msg.getBooleanValue("isQuote");
        postReply.setIsQuote(isQuote);
        if(isQuote) {
            JSONObject jsonQuote = JSONObject.parseObject(msg.getString("quote"));
            postReply.setPostToFloor(jsonQuote.getIntValue("quotePostFloor"));
            postReply.setPostToNickName(jsonQuote.getString("quotePostAuthorName"));
            postReply.setPostToPostId(jsonQuote.getLongValue("quotePostId"));
            postReply.setPostToUserId(jsonQuote.getLongValue("quotePostAuthorId"));
            postReply.setPostToMessage(jsonQuote.getString("quotePostMessage"));
        }

        postReply.setTopicId(getTopicId(msg));
        postReply.setUserId(getUserId(msg));
       
        return postReply;

    }
    
    private int getAgent(JSONObject msg) {
        return msg.getIntValue("agent");
    }
    
    public ClubIntegral getClubIntegral(JSONObject msg) {
        if (getAgent(msg) <= 0) {
            return petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST);
        } else {
            return petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST_WAP);
        }
    }
    
    public long getTopicId(JSONObject msg) {
        return msg.getLongValue("topicId");
    }
}
